import asyncio
import websockets

async def send_message(custom_headers):
    """Connects to the WebSocket server and sends messages with custom headers."""

    try:
        async with websockets.connect("ws://localhost:8765", extra_headers=custom_headers) as websocket:
            print("Connected to server!")

            while True:
                message = input("Enter message (or 'exit' to quit): ")
                if message.lower() == 'exit':
                     break
                await websocket.send(message)
                response = await websocket.recv()
                print(f"Received from server: {response}")
    except websockets.exceptions.ConnectionClosedError as e:
        print(f"Connection closed unexpectedly. Error: {e}")
    except ConnectionRefusedError:
        print("Server not reachable, please ensure it is running.")
    except Exception as e:
       print(f"An unexpected error occurred: {e}")

async def main():
   """Entry point for the client."""
   custom_headers = {
         "X-Custom-Header-1": "value1",
         "X-Custom-Header-2": "value2",
         "Authorization": "Bearer my_token"
   }
   await send_message(custom_headers)

if __name__ == "__main__":
    asyncio.run(main())